﻿@page "/Admin/UserManager"
@using JxAudio.Core.Entity
@using Microsoft.Extensions.Localization
@using System.Diagnostics.CodeAnalysis
@using JxAudio.Core.Service
@using JxAudio.Web.Validators
@using JxAudio.Web.Vo

@inject IStringLocalizer<UserManager> StringLocalizer
@inject IIconTheme IconTheme
@inject MessageService MessageService
@inject UserService UserService

<AdminTable TItem="UserEntity" ShowToolbar="true" ShowExtendButtons="true" OnSaveAsync="OnSaveAsync">
    <TableColumns>
        <TableColumn @bind-Field="@context.UserName"></TableColumn>
        <TableColumn @bind-Field="@context.Password" Visible="false" IsVisibleWhenEdit="false" IsVisibleWhenAdd="true" ComponentType="typeof(BootstrapPassword)"></TableColumn>
        <TableColumn @bind-Field="@context.IsAdmin"></TableColumn>
        <TableColumn @bind-Field="@context.IsGuest"></TableColumn>
        <TableColumn @bind-Field="@context.MaxBitRate"></TableColumn>
        <TableColumn @bind-Field="@context.CanJukebox"></TableColumn>
    </TableColumns>
    <RowButtonTemplate>
        <TableCellButton Text="@StringLocalizer["ChangePassword"]" OnClick="() => ChangePassword(context)"></TableCellButton>
    </RowButtonTemplate>
</AdminTable>

<Modal @ref="Modal">
    <ModalDialog IsCentered="true" Title="@StringLocalizer["ChangePassword"]" ShowFooter="false">
        <BodyTemplate>
            <ValidateForm @ref="ValidateForm" Model="_reSetPassword" OnValidSubmit="OnValidSubmit">
                <div class="row g-3">
                    <div class="col-12">
                        <BootstrapPassword @bind-Value="_reSetPassword!.NewPassword"></BootstrapPassword>
                    </div>
                    <div class="col-12">
                        <BootstrapPassword @bind-Value="_reSetPassword.RePassword" ValidateRules="_validators"></BootstrapPassword>
                    </div>
                </div>
                <div class="modal-footer">
                    <DialogCloseButton Text="@StringLocalizer["Close"]" Icon="@IconTheme.GetIconByKey(ComponentIcons.DialogCloseButtonIcon)"></DialogCloseButton>
                    <Button ButtonType="ButtonType.Submit" Color="Color.Primary" Text="@StringLocalizer["ChangePassword"]" Icon="@IconTheme.GetIconByKey(ComponentIcons.DialogSaveButtonIcon)" ></Button>
                </div>
            </ValidateForm>
        </BodyTemplate>
    </ModalDialog>
</Modal>

@code {
    
    [NotNull]
    private Modal? Modal { get; set; }
    
    [NotNull]
    private ValidateForm? ValidateForm { get; set; }

    private ResetPasswordVo? _reSetPassword;
    
    private readonly List<IValidator> _validators = [new RePasswordValidator()];

    private async Task ChangePassword(UserEntity userEntity)
    {
        _reSetPassword = new ResetPasswordVo()
        {
            UserEntity = userEntity
        };
        await Modal.Toggle();
    }

    private async Task OnValidSubmit(EditContext arg)
    {
        var userEntity = _reSetPassword!.UserEntity!;
        userEntity.Password = _reSetPassword.NewPassword;
        UserService.EncryptPassword(userEntity);
        await userEntity.SaveAsync();
        await Modal.Toggle();
        await MessageService.Show(new MessageOption()
        {
            Content = StringLocalizer["Success"]
        });
    }

    private async Task<bool> OnSaveAsync(UserEntity userEntity, ItemChangedType itemChangedType)
    {
        if (itemChangedType == ItemChangedType.Add)
        {
            UserService.EncryptPassword(userEntity);
        }

        await userEntity.SaveAsync();
        return true;
    }

}